/*
 * Copyright (c) 2020 Deutsches Elektronen-Synchroton,
 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program (see the file COPYING.LIB for more
 * details); if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.dcache.nfs.v4;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.dcache.nfs.v4.xdr.sequenceid4;
import org.dcache.nfs.v4.xdr.sessionid4;
import org.dcache.nfs.v4.xdr.slotid4;

/**
 * NFSv4.1 session on the client side.
 */
public class ClientSession {

    /**
     * session it provided by the server
     */
    private final sessionid4 id;

    /**
     * maximal number of concurrent requests the client can sent to on this session
     */
    private final int maxRequests;

    /**
     * Queue that maintains the available/unused sessions slots.
     */
    private final BlockingQueue<SessionSlot> unusedSessionSlots = new LinkedBlockingQueue<>();

    /**
     * session slot with associated id and sequence.
     */
    public static class SessionSlot {

        /**
         * slot id
         */
        private final slotid4 id;

        /**
         * requests sequence id
         */
        private int sequenceid = 0;

        public SessionSlot(int id) {
            this.id = new slotid4(id);
        }

        public slotid4 getId() {
            return id;
        }

        public sequenceid4 nextSequenceId() {
            return new sequenceid4(++sequenceid);
        }
    }

    /**
     * Create a new client side session with given is and maximal number of concurrent requests.
     *
     * @param sessionid session id generated by server.
     * @param maxRequests maximal number of concurrent requests.
     */
    public ClientSession(sessionid4 sessionid, int maxRequests) {

        this.id = sessionid;
        this.maxRequests = maxRequests;

        for (int i = 0; i < maxRequests; i++) {
            unusedSessionSlots.add(new SessionSlot(i));
        }
    }

    /**
     * Get available session slot, waiting if necessary until a sot becomes available.
     */
    public SessionSlot acquireSlot() throws IOException {
        try {
            return unusedSessionSlots.take();
        } catch (InterruptedException e) {
            InterruptedIOException eio = new InterruptedIOException(e.getMessage());
            // preserve the original stacktrace
            eio.setStackTrace(e.getStackTrace());
            throw eio;
        }
    }

    /**
     * Return session slot into pool of available slots.
     */
    public void releaseSlot(SessionSlot slot) {
        unusedSessionSlots.offer(slot);
    }

    /**
     * Get the session id.
     *
     * @return session id.
     */
    public sessionid4 sessionId() {
        return id;
    }

    /**
     * Get the maximal number of concurrent requests that can be sent to the server.
     *
     * @return maximal number of concurrent requests.
     */
    public int maxRequests() {
        return maxRequests;
    }
}
